<?php

namespace App\Models\School;

use App\Models\Article\ArticleInfo;
use App\Models\Common\CommonModel;
use App\Models\Rbac\GuardUser;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class School extends CommonModel
{
    use HasFactory;

    protected $table = 'school';

    protected $appends = ['url','logo'];

    public function getCountry() {
        return $this->hasOne(Country::class,'id','country_id');
    }

    public function getGuardUser() {
        return $this->hasOne(GuardUser::class,'id','guard_user_id');
    }

    public function getUrlAttribute() {
        return asset($this->pic_url);
    }

    public function getArticleInfo(){
        $records = ArticleInfo::where('school_id',$this->id)->where('status',1)
            ->orderBy('is_top')->orderByDesc('id')->limit(2)->get();

        foreach ($records as $record) {
            $record->url = asset($record->cover_url);
        }

        return $records;
    }

    public function getLogoAttribute() {
        return asset($this->logo_url);
    }
}
